package com.ruoyi.web.controller.shop;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SysGame;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysGameService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 *  店铺游戏管理 业务处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/shop/game")
public class ShopGameController extends BaseController
{
    @Autowired
    private ISysUserService userService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysGameService gameService;


    /**
     * 获取店铺信息
     */
    @PreAuthorize("@ss.hasPermi('shop:project:index')")
    @GetMapping("/index")
    public TableDataInfo getList(SysGame game)
    {
        startPage();
        //AjaxResult ajax = AjaxResult.success();
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();

        SysDept dept = deptService.selectDeptById(user.getDeptId());
        List<SysGame> games = gameService.selectGameByDeptId(game, dept.getDeptId());
        return getDataTable(games);
    }

    /**
     * 新增店铺游戏
     */
    @PreAuthorize("@ss.hasPermi('shop:project:add')")
    @Log(title = "游戏管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody List<Long> gameIds)
    {
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();

        deptService.insertGame(user.getDeptId(), gameIds);
        return AjaxResult.success("绑定成功");
    }

    /**
     * 删除游戏管理
     */
    @PreAuthorize("@ss.hasPermi('shop:project:remove')")
    @Log(title = "游戏管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{gameIds}")
    public AjaxResult remove(@PathVariable Long[] gameIds)
    {
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        return toAjax(deptService.deleteGameByGameIds(user.getDeptId(), gameIds));
    }

}
